package com.example.design.patterns.冰河.行为模式.迭代器模式.concreteAggregate;

import com.example.design.patterns.冰河.行为模式.迭代器模式.aggregate.List;
import com.example.design.patterns.冰河.行为模式.迭代器模式.concreteIterator.IteratorImpl;
import com.example.design.patterns.冰河.行为模式.迭代器模式.iterator.Iterator;

/**
 * ConcreteAggregate 具体聚合实现创建相应迭代器的接口,改操作返回 ConcreteIterator的一个适当的实例.
 */
public class ListImpl implements List {
    private Object[] list;
    private int index;
    private int size;

    public ListImpl() {
        this.index = 0;
        this.size = 0;
        this.list = new Object[100];
    }

    /**
     *
     * @return
     */
    @Override
    public Iterator Iterator() {
        return new IteratorImpl(this);
    }

    /**
     *
     * @param index
     * @return
     */
    @Override
    public Object get(int index) {
        return list[index];
    }

    /**
     *
     * @return
     */
    @Override
    public int getSize() {
        return this.size;
    }

    /**
     *
     * @param obj
     */
    @Override
    public void add(Object obj) {
        list[index++] = obj;
        size++;
    }
}
